package web;

import pojo.User;
import service.UserServcive;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegistServlet extends HttpServlet {
    private UserServcive  user =new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String repasw =req.getParameter("repwd");
        String email = req.getParameter("email");
        String code = req.getParameter("code");
        if ("6n6np".equals(code)){
            System.out.println(111);
            if (user.existUsername(username)){
                req.setAttribute("msg","用户名已存在");
                req.setAttribute("email",email);
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);
            }else{
                System.out.println(222);
                if(repasw.equals(password)){
                    user.register(new User(null,username,password,email));
                    System.out.println("注册成功！！");
                    req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req,resp);
                }
            }
        }else{
            req.setAttribute("username",username);
            req.setAttribute("password",password);
            req.setAttribute("repwd ",repasw);
            req.setAttribute("email",email);
            req.setAttribute("msg","验证码错误");
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);
        }
    }
}
